/***
This do-file creates a figure comparing the change in employment from Jan-Jul 
2020 using the tracker employment series vs the CES, by industry. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/data/derived/CES"
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Compare Paychex-Intuit-Earnin to CES by Industry
*-------------------------------------------------------------------------------

* Load Employment data
project, uses("${root}/data/dvc/Employment/Employment Combined BY national income naics.dta")
use "${root}/data/dvc/Employment/Employment Combined BY national income naics.dta", clear
keep date emp naics 

replace emp = (emp - 1) * 100

tostring naics, force replace
replace naics = "31-33" if naics == "31"
replace naics = "44-45" if naics == "44"
replace naics = "48-49" if naics == "48"

* Open CES by Industry
preserve
project, uses("${root}/data/derived/CES/national_industry_employment.dta")
use "${root}/data/derived/CES/national_industry_employment.dta", clear
gisid month naics 

drop if inlist(naics, "11", "91", "92")

assert year == 2020
gen date = mdy(month, 17, 2020)

gen temp = employment if month == 1
bys naics: gegen base_CES = mean(temp)
gen norm_employment_CES = (employment / base_CES - 1) * 100

keep norm_employment_CES naics date base_CES

gen Fridays = date - dow(date) + 5
format Fridays %td
drop date 
rename Fridays date

tempfile ces_industry
save `ces_industry'
restore

merge 1:1 date naics using `ces_industry', nogen keep(3)

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

corr emp norm_employment_CES [w = base_CES]  if date == td(17july2020) 
scalar corr = round(`r(rho)', 0.01)
local corr: di %4.2f scalar(corr)

* Calculate RMSE
gen error = emp - norm_employment_CES
gen error_sq = error ^ 2
sum error_sq if date == td(17july2020) [w = base_CES]
local rmse : di %4.2f sqrt(`r(mean)')

* Keep only observations on July 17
keep if date == td(17july2020)

tw  (lfit emp norm_employment_CES [w = base_CES] , lcolor(oi2)) ///
(scatter emp norm_employment_CES, mlabel(naics) msymbol(i) mlabcolor(oi1) mlabpos(0)) ,  ///
xlabel(-20 "-20%" -15 "-15%" -10 "-10%" -5 "-5%" 0 "0%") ///
ylabel(-30 "-30%" -20 "-20%" -10 "-10%" 0 "0%", nogrid) ///
legend(off) ///
text(-28.5 -2 "Correlation: `corr'" "RMSE: `rmse' p.p.", color(oi2) placement(east) justification(left)) ///
ytitle("Paychex-Intuit Change in Employment (%)" "from January to July 2020") xtitle("CES Change in Employment (%)" "from January to July 2020") ///
${title_`version'}
oi_graph_export "${root}/results/Employment/Employment Tracker vs CES by Industry", type(${fig_type})

*-------------------------------------------------------------------------------
* Output numbers
*-------------------------------------------------------------------------------

cap erase "${root}/results/paper numbers/`category'/Change in Employment Rates to July by 2-Digit NAICS Code Paychex-Intuit-Earnin vs CES.yaml"

* Export output numbers to csv file
yamlout using "${root}/results/paper numbers/`category'/Change in Employment Rates to July by 2-Digit NAICS Code Paychex-Intuit-Earnin vs CES.yaml", ///
	key("emp_ces_ind_corr") ///
	comment("Correlation") ///
	value(`corr') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Employment Rates to July by 2-Digit NAICS Code Paychex-Intuit-Earnin vs CES.yaml", ///
	key("emp_ces_ind_rmse") ///
	comment("RMSE") ///
	value(`rmse') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Change in Employment Rates to July by 2-Digit NAICS Code Paychex-Intuit-Earnin vs CES.yaml")
